function getContextPath(){ 
	var pathName = document.location.pathname; 
	var index = pathName.substr(1).indexOf("/"); 
	var result = pathName.substr(0,index+1); 
	return result; 
}

$(function(){
	
	var endTime = $("#endTime").text();
	
	$.validator.addMethod("checkDelayClerk",function(value){
		var pattern = /^[^0-9]{2,7}$/;
		return pattern.test(value);
	},"请输入合法姓名")
	
	$.validator.addMethod("checkPostTime",function(value){
		var myTime = Date.parse(endTime.toString());
		var ApplyTime = Date.parse(value);
		if (ApplyTime > myTime){
			return true;
		} else {
			return false;
		}
	},"延期到（日期）不得早于原任务结束日期")
	
	$.validator.addMethod("checkApplyTime",function(value){
		var myTime = Date.parse(new Date().toLocaleDateString());
		var ApplyTime = Date.parse(value);
		if (ApplyTime <= myTime){
			return true;
		} else {
			return false;
		}		
	},"申请日期不能大于当前日期")
	
	$("#regForm").validate({
		rules:{
			delayClerk:{
				required:true,
				rangelength:[2,7]
			},
			applyTime:{
				required:true,
				checkApplyTime:true
			},
			applyReason:{
				required:true
			},
			postTime:{
				required:true,
				checkPostTime:true
			}
		},
		messages:{
			delayClerk:{
				required:"申请人不能为空",
				checkDelayClerk:"请输入合法姓名"
			},
			applyTime:{
				required:"申请日期不能为空",
				checkApplyTime:"申请日期不能大于当前日期"
			},
			applyReason:{
				required:"延期原因不能为空"
			},
			postTime:{
				required:"延期到（日期）不能为空",
				checkPostTime:"延期到（日期）不得早于原任务结束日期"
			}
		},
		submitHandler:function(){
			var params = $("#regForm").serialize();
			var id = $("#taskId").text();
			$.post(getContextPath()+"/DelayTaskApply.oa?cmd=add&taskId="+id,params,function(data){
				if (data.code == 1){
					alert(data.msg);
					window.location.href=getContextPath()+"/task/IssuedTask.jsp";
				} else {
					alert(data.msg);
				}
			},"json")
		},
	})
})